<template>
	<div class="container">
		<!-- 路径导航区域 -->
		<el-breadcrumb separator="/">
			<el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
			<el-breadcrumb-item>收费分类管理</el-breadcrumb-item>
		</el-breadcrumb>
		<!-- 搜索区域 -->
		<el-row>
			<el-form :inline="true">
				<el-form-item label="收费类型">
					<el-input v-model="queryInfo.name" clearable @clear="queryList" placeholder="请输入收费类型"></el-input>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" @click="queryList">搜索</el-button>
					<el-button type="primary" @click="toAddChargetype">新增收费类型</el-button>
					<el-button type="danger" @click="deleteBatch">批量删除</el-button>
				</el-form-item>
			</el-form>
		</el-row>
		<!-- 新增和修改的对话框 -->
		<!-- dialog中绑定close事件，实现对话框右上角叉号的取消功能 -->
		<el-dialog :title="title" :visible.sync="dialogFormVisible" @close="cancle">
			<el-form :model="chargetype">
				<el-form-item label="收费类型" label-width="100px">
					<el-input placeholder="请输入收费类型" v-model="chargetype.name"></el-input>
				</el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button @click="cancle">取 消</el-button>
				<el-button type="primary" @click="addChargetype">确 定</el-button>
			</div>
		</el-dialog>

		<!-- 表格区域 -->
		<el-table ref="multipleTable" @selection-change="handleSelectionChange" :data="tableData" border
			style="width: 100%;" :header-cell-style="{'text-align': 'center'}" :cell-style="{'text-align': 'center'}">
			<el-table-column type="selection" width="55"></el-table-column>
			<el-table-column prop="id" label="ID" width="100"></el-table-column>
			<el-table-column prop="name" label="收费类型">
			</el-table-column>
			<el-table-column label="操作" width="100">
				<template slot-scope='scope'>
					<el-link type="primary" @click="toEditChargetype(scope.row)">编辑</el-link><br />
					<el-link type="primary" @click="deleteChargetype(scope.row.id)">删除</el-link>
				</template>
			</el-table-column>
		</el-table>
		<!-- 分页区域 -->
		<el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
			:current-page="queryInfo.pageNum" :page-sizes="[1, 5, 10, 20]" :page-size="queryInfo.pageSize"
			layout="total, sizes, prev, pager, next, jumper" :total="total">
		</el-pagination>
	</div>
</template>
<script>
	export default {
		data() {
			return {
				// 查询信息对象
				queryInfo: {
					name: '',
					pageNum: 1,
					pageSize: 5
				},
				total: 10,
				// 表格数据
				tableData: [],
				// 批量删除
				ids: null,
				// 对话框标题
				title: '',
				// 是否显示对话框
				dialogFormVisible: false,
				// 收费类型对象 chargetype
				chargetype: {
					"id": '',
					"name": ""
				},
			}
		},
		methods: {
			// 分页查询信息的方法
			queryList() {
				// 调用后端接口来获取数据
				this.$http.get('/chargetype/find', {
						params: this.queryInfo
					})
					.then(resp => {
						if (resp.data.status == 200) {
							// console.log(resp.data)
							this.tableData = resp.data.data.list;
							this.total = resp.data.data.total;
							// console.log(this.tableData)
						} else {
							this.$message.error('查询失败！')
						}
					})
					.catch((error) => {
						this.$message.error('服务器失联' + error)
					})
			},
			// 进入添加的对话框
			toAddChargetype() {
				this.setAddress(this.address)
				this.title = "新增收费类型";
				this.dialogFormVisible = true;
			},
			// 进入修改的预览数据页面
			toEditChargetype(row) {
				// console.log(row)
				// 赋值
				this.chargetype = row;
				// 设置标题
				this.title = "修改收费类型";
				// 开启对话框
				this.dialogFormVisible = true;
			},
			// 添加
			addChargetype() {
				console.log("添加收费类型")
				if (this.chargetype.id) { // 有值为true  修改
					this.setAddress(this.address)
					this.$http.put("/chargetype/edit", this.chargetype)
						.then(resp => {
							if (resp.data.status == 200) {
								// 消息提示
								this.$message.success('修改成功！');
								// 清空数据
								this.chargetype = { // 对象
									"id": '',
									"name": "",
								}
								// 关闭对话框
								this.dialogFormVisible = false;
								// 重新查询
								this.queryList()
							} else {
								this.$message.error('接口异常！')
							}
						})
				} else {
					this.$http.post("/chargetype/add", this.chargetype)
						.then(resp => {
							if (resp.data.status == 200) {
								// 消息提示
								this.$message.success('添加成功！');
								// 清空数据
								this.chargetype = { // 对象
									"id": '',
									"name": "",
								}
								// 关闭对话框
								this.dialogFormVisible = false;
								// 重新查询
								this.queryList()
							} else {
								this.$message.error('接口异常！')
							}
						})
				}
			},
			// 取消对话框
			cancle() {
				this.dialogFormVisible = false;
				this.chargetype = { // 对象
					"id": '',
					"name": ""
				}
			},
			// 删除收费类型
			deleteChargetype(id) {
				this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
						confirmButtonText: '确定',
						cancelButtonText: '取消',
						type: 'warning'
					})
					.then(() => {
						this.$http.delete('/chargetype/remove/' + id)
							.then(resp => {
								if (resp.data.status == 200) {
									this.$message.success(resp.data.message)
									this.queryList()
								} else {
									this.$message.error(resp.data.message)
								}
							})
					})
					.catch(() => {
						this.$message({
							type: 'info',
							message: '已取消删除'
						})
					})
			},
			// 批量删除
			handleSelectionChange(val) {
				this.ids = val.map(function(item) {
					return item.id;
				});
				// console.log(this.ids);
			},
			// 批量删除收费类型
			deleteBatch() {
				if (this.ids.length == 0) { // 没有选择收费类型列表
					return;
				}
				this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
						confirmButtonText: '确定',
						cancelButtonText: '取消',
						type: 'warning'
					})
					.then(() => {
						this.$http.delete('/chargetype/removeBatch?ids=' + this.ids)
							.then(resp => {
								if (resp.data.status == 200) {
									this.$message.success(resp.data.message)
									this.queryList()
								} else {
									this.$message.error(resp.data.message)
								}
							})
					})
					.catch(() => {
						this.$message({
							type: 'info',
							message: '已取消删除'
						})
					})
			},
			// 分页
			handleSizeChange(size) {
				//console.log('每页显示数量发生改变');
				this.queryInfo.pageSize = size;
				this.queryInfo.pageNum = 1;
				// 重新查询
				this.queryList()
			},
			handleCurrentChange(num) {
				//console.log('页码发生改变');
				this.queryInfo.pageNum = num;
				// 重新查询
				this.queryList();
			},
		},
		mounted() {
			this.queryList();
			// 动态的获取tool.js中的url，并赋值给当前组件的全局url
			// this.url = url;
			// 原型链引入
			this.url = this.$url;
		},
	}
</script>
<style>
	.el-breadcrumb {
		margin-bottom: 15px;
		padding: 0 0 15px;
		border-bottom: 1px solid #ddd;
	}

	.el-pagination {
		margin-top: 15px;
	}

	.el-link {
		padding: 0 5px;
	}

	/**
	 * 设置富文本编辑器的最小高度，注意style中不能使用scoped 
	**/
	.ql-container {
		min-height: 200px;
	}
</style>